package com.mathdroid.model.enums;

import java.util.LinkedList;

import com.mathdroid.exceptionhandling.ParserException;

/**
 * Each character class has partially different item slots.
 */
public enum EItemSlot {
	
	/**
	 * No Slot. Some classes have no secondary Offhand slot.
	 */
	NONE(null, null),
	/**
	 * Ear.
	 */
	EAR("Ear", "ear"),
	/**
	 * Implant 1.
	 */
	IMPLANT_1("Implant", "implant"),
	/**
	 * Implant 2.
	 */
	IMPLANT_2("Implant", "implant"),
	/**
	 * Wrists.
	 */
	WRISTS("Wrists", "wrists"),
	/**
	 * Relic 1.
	 */
	RELIC_1("Relic", "relic"),
	/**
	 * Relic 2.
	 */
	RELIC_2("Relic", "relic"),
	/**
	 * Head.
	 */
	HEAD("Head", "head"),
	/**
	 * Chest.
	 */
	CHEST("Chest", "chest"),
	/**
	 * Hands.
	 */
	HANDS("Hands", "hands"),
	/**
	 * Waist.
	 */
	WAIST("Waist", "waist"),
	/**
	 * Legs.
	 */
	LEGS("Legs", "legs"),
	/**
	 * Feet.
	 */
	FEET("Feet", "feet"),
	/**
	 * Main Hand.
	 */
	MELEE_MAIN_HAND("Melee Main Hand", "meleemainhand"),
	/**
	 * Offhand.
	 */
	MELEE_OFF_HAND("Melee Off Hand", "meleeoffhand"),
	/**
	 * Knife/Melee.
	 */
	KNIFE_MELEE("Knife/Melee", "knivemelee"),
	/**
	 * Ranged Main Hand.
	 */
	RANGED_MAIN_HAND("Ranged Main Hand", "rangedmainhand"),
	/**
	 * Ranged Off Hand.
	 */
	RANGED_OFF_HAND("Ranged Off Hand", "rangedoffhand"),
	/**
	 * Scattergun/Shotgun.
	 */
	SCATTERGUN_SHOTGUN("Scattergun/Shotgun", "scattergunshotgun");
	
	/**
	 * The description of the slot.
	 */
	private String name = null;
	
	/**
	 * The icon name.
	 */
	private String icon = null;
	
	/**
	 * Create a slot enumeration.
	 * @param n Name.
	 * @param i Icon name.
	 */
	EItemSlot(final String n, final String i) {
		this.name = n;
		this.icon = i;
	}
	
	@Override
	public final String toString() {
		return this.name;
	}
	
	/**
	 * Get all item slots. <b>Important:</b> The slots are in no specific order.
	 * @return Item slots.
	 */
	public static final LinkedList<EItemSlot> getAllSlots() {
		LinkedList<EItemSlot> slots = new LinkedList<EItemSlot>();
		
		/* Those slots are shared between classes. */
		slots.addAll(getArmorSlots());
		
		/* Mainhand and Offhand slots are different for the classes. */
		slots.addAll(getWeaponSlots());
		
		return slots;
	}

	/**
	 * Get the name of the icon.
	 * @return The icon name.
	 */
	public String getIcon() {
		return this.icon;
	}
	
	/**
	 * Parse a string to an enumeration object. The parser compares the
	 * string representation of this enumeration with the parameter, not the
	 * literal.
	 * @param t String.
	 * @return EItemSlot object.
	 * @throws ParserException The string cannot be parsed.
	 */
	public static final EItemSlot parseEItemSlot(final String t) 
	throws ParserException {
		EItemSlot s = null;
		
		for (EItemSlot src : EItemSlot.getAllSlots()) {
			if (t.equals(src.toString())) {
				s = src;
			}
		}
		
		if (s == null) {
			ParserException p = new ParserException("No such slot: \"" 
					+ t + "\"");
			p.fillInStackTrace();
			throw p;
		}
		
		return s;
	}

	/**
	 * Get the armor slots.
	 * @return Armor slots.
	 */
	public static LinkedList<EItemSlot> getArmorSlots() {
		LinkedList<EItemSlot> slots = new LinkedList<EItemSlot>();
		
		/* Left side. */
		slots.add(EItemSlot.EAR);
		slots.add(EItemSlot.IMPLANT_1);
		slots.add(EItemSlot.IMPLANT_2);
		slots.add(EItemSlot.WRISTS);
		slots.add(EItemSlot.RELIC_1);
		slots.add(EItemSlot.RELIC_2);
		
		/* Right side */
		slots.add(EItemSlot.HEAD);
		slots.add(EItemSlot.CHEST);
		slots.add(EItemSlot.HANDS);
		slots.add(EItemSlot.WAIST);
		slots.add(EItemSlot.LEGS);
		slots.add(EItemSlot.FEET);
		
		return slots;
	}

	/**
	 * Get the weapon slots.
	 * @return Weapon slots.
	 */
	public static LinkedList<EItemSlot> getWeaponSlots() {
		LinkedList<EItemSlot> slots = new LinkedList<EItemSlot>();

		slots.add(EItemSlot.MELEE_MAIN_HAND);
		slots.add(EItemSlot.MELEE_OFF_HAND);
		slots.add(EItemSlot.RANGED_MAIN_HAND);
		slots.add(EItemSlot.RANGED_OFF_HAND);
		slots.add(EItemSlot.KNIFE_MELEE);
		slots.add(EItemSlot.SCATTERGUN_SHOTGUN);
		
		return slots;
	}
}
